<?php 

class EditorHelper extends AppHelper
{
	var $sourceJs;

	var $editorBasePath = '';

	var $helpers = array('Html','Form','Javascript');

	var $outputJsFunction;


	function __construct()
	{
		
		$this->selectedEditor = "TinyMCE";
		if(cr('Editor.defaultEditor'))
		{
			$this->selectedEditor = cr('Editor.defaultEditor');
		}
		
		if($this->selectedEditor == "FCKEditor")
		{
			// check if installed at webroot
			$this->editorBasePath = Configure::read('cmsConfig.Backend.siteUrl')."/fck_editor/";
			$this->editorFilePath = WWW_ROOT."fck_editor";
			$this->sourceJs = $this->editorBasePath.'fckeditor.js';
			$this->outputJsFunction = 'outputFck';
		}
		if($this->selectedEditor == "TinyMCE")
		{
			// check if installed at webroot
			$this->editorBasePath = Configure::read('cmsConfig.Backend.siteUrl')."/tiny_mce/";
			$this->editorFilePath = WWW_ROOT."tiny_mce";
			$this->sourceJs = $this->editorBasePath.'tiny_mce.js';
			$this->outputJsFunction = 'outputTinyMCE';
		}
		if($this->selectedEditor == "CkEditor")
		{
			$this->outputJsFunction = 'outputCk';
			$this->editorFilePath = WWW_ROOT."ckeditor";
		}
		// check if editor exists at base path
		if(!is_dir($this->editorFilePath))
		{
			trigger_error("Please copy ".$this->selectedEditor." editor at ".$this->editorFilePath." before using it.");
			die();
		}
	}


	function outputFck($class, $options)
	{

		echo <<<FCK
<script type="text/javascript" src="$this->sourceJs"></script>
<SCRIPT>
$(document).ready(function(){
	var sBasePath = "$this->editorBasePath";
	$('.$class').each(function(){
		var oFCKeditor = new FCKeditor(this.name) ;
		oFCKeditor.Height = 400 ;
		oFCKeditor.BasePath	= sBasePath ;
		oFCKeditor.ReplaceTextarea() ;
		});	
	});	
</SCRIPT>
FCK;

	}

	function outputTinyMCE($class, $options)
	{
		$siteUrl = Configure::read('cmsConfig.Backend.siteUrl');

		$lists = '';
		// build static pages list if plugin exists
		$contentCss = '';
		if(isInstalled('static_pages') && strpos($_SERVER['REQUEST_URI'],'admin')==true)
		{
			$lists .= 'external_link_list_url : "'.$siteUrl.'/admin/static_pages/jsIndex",';
			$contentCss = 'content_css : "'.$siteUrl.'/css/style.css",';
		}
		$type = "advanced";
		if($options['editor_type']!='advance')
			$type= "simple";
		$editorJS = '
<script type="text/javascript" src="'.$this->sourceJs.'"></script>
<SCRIPT>

var Modules = [{"heading":"HTML Box", "module_name":"cms_htmlBox","icon":"\/cms\/img\/help.gif"},{"heading":"HTML Box 2", "module_name":"cms_htmlBox1","icon":"\/cms\/img\/calendar.gif"}];
cmsToolbar = "";
    $.each(Modules, function(i,item){
		cmsToolbar = cmsToolbar + "," + item.module_name;
	});
         
	tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "'.$type.'",
		editor_selector : "'.$class.'",
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager",
		extended_valid_elements : "module[*]",
		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|"+cmsToolbar+",charmap,emotions,iespell,media,advhr,|,print,insertimage",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		
		relative_urls : false,
		document_base_url : "'.$siteUrl.'",


		// Example content CSS (should be your site CSS)
		'.$contentCss.'
		// Drop lists for link/image/media/template dialogs
		'.$lists.'

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
</SCRIPT>';
		$view =& ClassRegistry::getObject('view');
		$view->addScript('editorJS', $editorJS);

		// $editorJS

	}

	function outputJs($class, $options)
	{
		$outputFn = $this->outputJsFunction;
		$defaultOptions = array('editor_type'=>'advance');
		$options = am($defaultOptions,$options);
		$this->{$outputFn}($class, $options);
	}
	
	function outputCk($class,$options)
	{
		App::import('Vendor','Editor.CkEditor');
		$editorObj = null;
		if(isset($options['editor']) && is_object($options['editor']))
		{
			$editorObj = $options['editor'];		
		}else
		{
			$type= 'basic';
			if(isset($options['editor']))
			{
				$type = $options['editor'];
			}
			$editorObj = new CkEditor();
			$editorObj->setToolbar($type);
			$editorObj->setDomSelector('#'.$options['id']);
					
		}
		$editorJs = $editorObj->getEditor();
		if(!isset($this->Javascript) || !is_object($this->Javascript))
		{
			$this->initHelper('Javascript');
		}
		//$jsFiles = CkEditor::getJsFiles();
		//$this->Javascript->link($jsFiles,false);
		CkEditor::includeJs();
		$view =& ClassRegistry::getObject('view');
		$this->Javascript->startup( $editorJs);
	}
}
?>